﻿// <copyright file="MapViewer.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains MapViewer, implementation of IMapViewer.</summary>

namespace Sqwarea.GameLogic.View
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using Sqwarea.Persistence;

    /// <summary>
    /// MapViewer class, implementation of IMapViewer
    /// </summary>
    public class MapViewer : IMapViewer
    {
        /// <summary>
        /// The Persistence GameProvider
        /// </summary>
        private IGameProvider _gameProvider;

        /// <summary>
        /// Initializes a new instance of the MapViewer class.
        /// </summary>
        /// <param name="gameProvider">The Persistence GameProvider</param>
        public MapViewer(IGameProvider gameProvider)
        {
            if (null == gameProvider)
            {
                throw new ArgumentNullException("gameProvider");
            }

            _gameProvider = gameProvider;
        }

        /// <summary>
        /// Gets the view of kingId user related to the rectangle.
        /// </summary>
        /// <param name="kingId">Id of the required king</param>
        /// <param name="rectangle">Rectangle related to the view</param>
        /// <returns>See summary</returns>
        public UserRectangleView GetView(long kingId, Rectangle rectangle)
        {
            // Rectangle is a struct, "requestRect" is a real copy of rectangle (not a reference)
            Rectangle requestRect = rectangle;
            requestRect.Inflate(1, 1);

            Square[,] squares = this._gameProvider.GetSquaresInRectangle(requestRect);

            var kingsCache = new KingsCache(this._gameProvider);

            return new UserRectangleView(kingId, squares, rectangle, kingsCache);
        }

        /// <summary>
        /// Gets the incoming attacks to kingId user (i.e. attacks against this king's squares)
        /// </summary>
        /// <param name="kingId">Id of the required king</param>
        /// <returns>See summary</returns>
        public IEnumerable<AttackView> GetIncomingAttacks(long kingId)
        {
            var kingsCache = new KingsCache(_gameProvider);
            return from attack in this._gameProvider.GetIncomingAttacks(kingId)
                   select new AttackView(attack, kingsCache, false);
        }

        /// <summary>
        /// Gets the ongoing attacks og kingId user (i.e. attacks sended by this king).
        /// </summary>
        /// <param name="kingId">Id of the required king</param>
        /// <returns>See summary</returns>
        public IEnumerable<AttackView> GetOngoingAttacks(long kingId)
        {
            var kingsCache = new KingsCache(_gameProvider);
            return from attack in this._gameProvider.GetOngoingAttacks(kingId)
                   select new AttackView(attack, null, true);
        }
    }
}
